package org.itboys.mobile.controller.weixin;

import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.itboys.commons.CommonConstants;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.dto.SundayAddressDto;
import org.itboys.mobile.dto.SundayMemberSessionDto;
import org.itboys.mobile.dto.count.SundayCountTotalDto;
import org.itboys.mobile.service.mongo.front.SundayFrontAddressService;
import org.itboys.mobile.service.mongo.front.SundayFrontCountService;
import org.itboys.mobile.util.AccountIdHoldUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 作者：jiangxiong
 * 日期：2017年2月9日
 * 联系方式 ：empty90911@126.com
 * 描述：圣代_结算（确认订单）_controller。
 *
 */
@Controller
@RequestMapping("/sunday/weixin/count")
public class SundayWeiXinCountController extends BaseController {
    @Autowired
    private SundayFrontCountService countService;
    @Autowired
    private SundayFrontAddressService addressService;
    /**
     * 【微信端】购物车结算（确认订单）。针对：1普通，2新品，3特卖。如果使用了爱购币，余额。请重新调用接口加载数据
     * @param request
     * @param response
     * @param cartIds
     * @param isUseAiGou
     * @param isUseBalance
     */
    @RequestMapping(value="/countCart",method= {RequestMethod.POST, RequestMethod.GET})
    @ApiOperation(value="【微信端】购物车结算（确认订单）。针对：1普通，2新品，3特卖。如果使用了爱购币，余额,贝壳。请重新调用接口加载数据")
    public String countCart(HttpServletRequest request,
        HttpServletResponse response,
        Model model,
        @ApiParam(required = true, name = "cartIds", value = "购物车ID集合(非商品ID)。如：cardIds=1,2,3  【参与签名】") @RequestParam(value = "cartIds", required = true) List<Long> cartIds,
        @ApiParam(required = false, name = "isUseAiGou", value = "是否使用爱购币【shop==0，自营才能使用】。0否默认，1是。type==6或type==10隐藏") @RequestParam(value="isUseAiGou",required=false) Integer isUseAiGou,
        @ApiParam(required = false, name = "isUseBalance", value = "是否使用余额【shop==0，自营才能使用】。0否默认，1是。type==6或type==10隐藏") @RequestParam(value="isUseBalance",required=false) Integer isUseBalance,
        @ApiParam(required = false, name = "isUseConch", value = "是否使用贝壳【shop==0，自营才能使用】。0否默认，1是。type==6或type==10隐藏") @RequestParam(value="isUseConch",required=false) Integer isUseConch,
        @ApiParam(required = false, name = "teamIsJoin", value = "[拼团专用]是否参与拼团。0单独购买，1参与别人的，2自己开团") @RequestParam(value="teamIsJoin",required=false) Integer teamIsJoin,
        @ApiParam(required = false, name = "teamGroupId", value = "[拼团专用]要拼团的组ID。和[是否参与拼团==1]搭配使用。") @RequestParam(value="teamGroupId",required=false) Long teamGroupId,
        @ApiParam(required = false, name = "addressId", value = "收货地址ID。") @RequestParam(value="addressId",required=false) Long addressId,
        @ApiParam(required = false, name = "sendType", value = "配送方式。至针对自营。1，门店配送（默认），2自提") @RequestParam(value="sendType",required=false) Integer sendType){
        try{
            SundayMemberSessionDto memberSessionDto = AccountIdHoldUtils.getSessionMember(request);
            SundayCountTotalDto result = countService.countCart(memberSessionDto.getId(),cartIds,isUseAiGou,isUseBalance,isUseConch,teamIsJoin,teamGroupId,sendType);
            model.addAttribute("result",result);
            //2018年1月29日
            List<SundayAddressDto> addresses = addressService.select(memberSessionDto.getId());

            if(ListUtil.isNotNull(addresses)){
                model.addAttribute("address",addresses.get(0));
            }
            //2018年1月29日，将结算参数返回页面，以便结算是选择收货地址
            String countParam = "";
            StringBuilder cartIdsStr=new StringBuilder();
            for(Long productId:cartIds){
                cartIdsStr.append(productId+",");
            }
            countParam+="?cartIds="+cartIdsStr.substring(0,cartIdsStr.length()-1).toString();
            countParam+="&isUseAiGou="+(isUseAiGou==null?0:isUseAiGou);
            countParam+="&isUseBalance="+(isUseBalance==null?0:isUseBalance);
            countParam+="&isUseConch="+(isUseConch==null?0:isUseConch);
            countParam+="&teamIsJoin="+(teamIsJoin==null?0:teamIsJoin);
            countParam+="&teamGroupId="+(teamGroupId==null?0:teamGroupId);
            countParam+="&sendType="+(sendType==null?1:sendType);
            model.addAttribute("countParam",countParam);
            return "/aigou/weixin/count/index";
        }catch (Exception e){
            logger.error("/sunday/weixin/count/countCart----e="+e.getMessage());
            e.printStackTrace();
            return "/aigou/weixin/error";
        }
    }
}
